import pandas
from minepy import MINE


def corr(var,keyData):
    """
    计算特征与关键变量的相关性系数
    @param var: 特证值 =====>DataFrame
    @param keyData:  标签  =====>DataFrame
    @return: 相关性系数列表
    """
    corrCoeff=[]
    for i,col in enumerate(var.columns):
        s= var[col].corr(keyData)
        corrCoeff.append(s)
    # df = pd.DataFrame(corrCoeff,columns=['corr'])
    # df.to_excel("Zhengqi_Analysis1.xlsx",sheet_name="corr")
    return corrCoeff

def mic(var,keyData):
    """
    计算特征与关键变量的mic系数
    @param var:  特证值 =====>DataFrame
    @param keyData: 标签  =====>DataFrame
    @return: mic系数列表
    """
    micList= []
    for i,col in enumerate(var.columns):
        mine = MINE(alpha = 0.7,c=15)
        mine.compute_score(var[col],keyData)
        micList.append(mine.mic())
    # df = pd.DataFrame(micList, columns=['micList'])
    # df.to_excel("Zhengqi_Analysis2.xlsx", sheet_name="mic")
    return micList


def copula(var,keyData):
    """
    计算特征与主要变量的copula熵
    @param var: 非线性特征选择
    @return: copula_list copula
    """
    copula_list = []
    for i,col in enumerate(var.columns):

        data = pandas.concat([var[col],keyData],axis=1)
        copula_data=copent.copent(data)
        copula_list.append(copula_data)
    # df = pd.DataFrame(copula_list, columns=['copula'])
    # df.to_excel("Zhengqi_Analysis.xlsx", sheet_name="copula")
    return copula_list

